package com.example.mymvvmdemo.models.add_device.fragment.regisger_device;

import android.text.Editable;
import android.text.TextWatcher;
import android.view.View;

import com.example.mymvvmdemo.R;
import com.example.mymvvmdemo.base.BaseLoadFragment;
import com.example.mymvvmdemo.constants.UrlConstants;
import com.example.mymvvmdemo.constants.UrlTypeConstants;
import com.example.mymvvmdemo.databinding.FragmentRegisterDeviceBinding;
import com.example.mymvvmdemo.models.add_device.AddDeviceViewModel;
import com.example.mymvvmdemo.models.add_device.dialog.SwitchDeviceTypeDialog;
import com.example.mymvvmdemo.models.add_device.entity.DeviceTypeListEntity;
import com.example.mymvvmdemo.net.presenter.GetPresenter;
import com.example.mymvvmdemo.net.presenter.PostBodyPresenter;
import com.example.mymvvmdemo.net.view.IView;
import com.example.mymvvmdemo.utils.StrUtils;
import com.example.mymvvmdemo.utils.ToastUtils;

import java.util.ArrayList;
import java.util.HashMap;

public class RegisterDeviceFragment extends BaseLoadFragment<FragmentRegisterDeviceBinding> implements IView.GetView, View.OnClickListener, IView.PostBodyView {

    private AddDeviceViewModel viewModel;
    private GetPresenter getPresenter;
    private PostBodyPresenter postBodyPresenter;
    private SwitchDeviceTypeDialog switchDeviceTypeDialog;

    @Override
    protected void initData() {
        getPresenter = new GetPresenter(this);
        postBodyPresenter = new PostBodyPresenter(this);
    }

    @Override
    protected void initListener() {
        binding.clType.setOnClickListener(this);
        binding.ivClearName.setOnClickListener(this);
        binding.ivClearId.setOnClickListener(this);
        binding.ivClearMac.setOnClickListener(this);
        binding.ivClearCode.setOnClickListener(this);
        binding.tvNext.setOnClickListener(this);

        binding.etName.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence charSequence, int i, int i1, int i2) {

            }

            @Override
            public void onTextChanged(CharSequence charSequence, int i, int i1, int i2) {
                binding.ivClearName.setVisibility(charSequence.toString().length() > 0
                        ? View.VISIBLE : View.GONE);
            }

            @Override
            public void afterTextChanged(Editable editable) {

            }
        });
        binding.etId.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence charSequence, int i, int i1, int i2) {

            }

            @Override
            public void onTextChanged(CharSequence charSequence, int i, int i1, int i2) {
                binding.ivClearId.setVisibility(charSequence.toString().length() > 0
                        ? View.VISIBLE : View.GONE);
            }

            @Override
            public void afterTextChanged(Editable editable) {

            }
        });
        binding.etMac.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence charSequence, int i, int i1, int i2) {

            }

            @Override
            public void onTextChanged(CharSequence charSequence, int i, int i1, int i2) {
                binding.ivClearMac.setVisibility(charSequence.toString().length() > 0
                        ? View.VISIBLE : View.GONE);
            }

            @Override
            public void afterTextChanged(Editable editable) {

            }
        });
        binding.etCode.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence charSequence, int i, int i1, int i2) {

            }

            @Override
            public void onTextChanged(CharSequence charSequence, int i, int i1, int i2) {
                binding.ivClearCode.setVisibility(charSequence.toString().length() > 0
                        ? View.VISIBLE : View.GONE);
            }

            @Override
            public void afterTextChanged(Editable editable) {

            }
        });
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.iv_clear_name:
                binding.etName.setText("");
                break;
            case R.id.iv_clear_id:
                binding.etId.setText("");
                break;
            case R.id.iv_clear_mac:
                binding.etMac.setText("");
                break;
            case R.id.iv_clear_code:
                binding.etCode.setText("");
                break;
            case R.id.cl_type:
                //设备列表弹框
                if (switchDeviceTypeDialog != null) {
                    switchDeviceTypeDialog.show();
                }
                break;
            case R.id.tv_next:
                startBindDevice();
                break;
            default:
                break;
        }
    }

    @Override
    public void initDialog() {
        super.initDialog();

        switchDeviceTypeDialog = new SwitchDeviceTypeDialog(getContext());
        switchDeviceTypeDialog.setListener(new SwitchDeviceTypeDialog.PromptClickListener() {
            @Override
            public void onCancelClick() {

            }

            @Override
            public void onConfirmClick(String type, String name) {
                switchDeviceTypeDialog.close();
                binding.tvType.setText(name);
            }
        });
        startLoadDeviceList();

    }

    /***********************************自定义方法**************************************/


    public void startLoadDeviceList() {
        showDialog();
        HashMap<String, String> hashMap = new HashMap<>();
        getPresenter.startLoadGet(UrlConstants.deviceTypeList, hashMap, UrlTypeConstants.deviceTypeList);
    }

    public void setViewModel(AddDeviceViewModel viewModel) {
        this.viewModel = viewModel;
    }

    private void startBindDevice() {

        String name = binding.etName.getText().toString();
        String id = binding.etId.getText().toString();
        String mac = binding.etMac.getText().toString();
        String code = binding.etCode.getText().toString();
        String type = switchDeviceTypeDialog.getType();

        viewModel.name = name;
        viewModel.id = id;
        viewModel.mac = mac;
        viewModel.code = code;
        viewModel.type = type;

        if (StrUtils.isEmpty(name)) {
            toast(R.string.add_device_name, ToastUtils.TOA_W);
            return;
        }
        if (StrUtils.isEmpty(id)) {
            toast(R.string.add_device_id, ToastUtils.TOA_W);
            return;
        }
        if (StrUtils.isEmpty(mac)) {
            toast(R.string.add_device_mac, ToastUtils.TOA_W);
            return;
        }
        if (StrUtils.isEmpty(code)) {
            toast(R.string.add_device_code, ToastUtils.TOA_W);
            return;
        }
        if (StrUtils.isEmpty(type)) {
            toast(R.string.add_device_type, ToastUtils.TOA_W);
            return;
        }

//        viewModel.startBindDeviceActivity(getActivity(), name, id, mac, code, type);
        startRegisterDevice();
    }

    private void startRegisterDevice() {
        showDialog();
        HashMap<String, String> hashMap = new HashMap<>();
        hashMap.put("name", viewModel.name);
        hashMap.put("device_id", viewModel.id);
        hashMap.put("device_type", viewModel.type);
        hashMap.put("verify_code", viewModel.code);
        hashMap.put("mac_address", viewModel.mac);
        postBodyPresenter.startLoadPostBody(UrlConstants.registerWithCode, hashMap, UrlTypeConstants.registerWithCode);
    }

    /***********************************数据请求**************************************/
    @Override
    public void loadGetSuccess(String result, String type, int code) {
        if (type.equals(UrlTypeConstants.deviceTypeList)) {
            loadDeviceTypeListSuccess(result, code);
        }
    }

    @Override
    public void loadGetFail(String message, String type) {
        if (type.equals(UrlTypeConstants.deviceTypeList)) {
            loadDeviceTypeListFail();
        }
    }

    @Override
    public void loadGetAccountError(int code) {

    }

    @Override
    public void loadGetNetWorkError() {
        dismissDialog();
        toast(R.string.network_error, ToastUtils.TOA_W);
    }

    @Override
    public void loadPostBodySuccess(String result, String type, int code) {
        if (type.equals(UrlTypeConstants.registerWithCode)) {
            loadRegisterWithCodeSuccess(result, code);
        }
    }

    @Override
    public void loadPostBodyFail(String message, String type) {
        if (type.equals(UrlTypeConstants.registerWithCode)) {
            loadRegisterWithCodeFail();
        }
    }

    @Override
    public void loadPostBodyAccountError(int code) {

    }

    @Override
    public void loadPostBodyNetWorkError(String type) {
        dismissDialog();
        toast(R.string.network_error, ToastUtils.TOA_W);
    }


    /***********************************处理请求后数据**************************************/
    private void loadRegisterWithCodeSuccess(String result, int code) {
        if (code != 200) {
            loadRegisterWithCodeFail();
            return;
        }
        dismissDialog();
        toast(R.string.add_device_register_success, ToastUtils.TOA_S);
    }

    private void loadRegisterWithCodeFail() {
        dismissDialog();
        toast(R.string.add_device_register_fail, ToastUtils.TOA_F);
    }

    private void loadDeviceTypeListSuccess(String result, int code) {
        if (code != 200) {
            loadDeviceTypeListFail();
            return;
        }
        dismissDialog();
        DeviceTypeListEntity deviceListEntity = gson.fromJson(result, DeviceTypeListEntity.class);
        DeviceTypeListEntity.DataDTO data = deviceListEntity.getData();
        if (data == null) {
            loadDeviceTypeListFail();
            return;
        }
        ArrayList<DeviceTypeListEntity.DataDTO.DeviceTypesDTO> deviceTypes = data.getDevice_types();
        if (deviceTypes == null) {
            loadDeviceTypeListFail();
            return;
        }
        switchDeviceTypeDialog.setList(deviceTypes);

    }

    private void loadDeviceTypeListFail() {
        dismissDialog();
    }


}
